<template>
  <div class="app-container">
    <div class="app-container-content permission">
      <!-- 操作区域 -->
      <div class="btn">
        <div class="left">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click.native="handleAdd()">新增</el-button>
          <!-- <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDel()">批量删除</el-button> -->
        </div>
        <div class="right">
          <el-input v-model="filters.name" style="width: 210px" clearable placeholder="权限名称" />
          <el-input v-model="filters.pname" style="width: 210px" clearable placeholder="父权限名称" />
          <el-select v-model="filters.type" style="width: 210px" clearable placeholder="权限类型">
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
            <!-- <el-option :key="0" label="按照父权限筛选" value="0"></el-option> -->
          </el-select>
          <el-select v-model="filters.hidden" style="width: 210px" clearable placeholder="是否隐藏">
            <el-option
              v-for="item in hiddenOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
            <!-- <el-option :key="0" label="按照父权限筛选" value="0"></el-option> -->
          </el-select>
          <el-button icon="el-icon-search" @click="doFilter()">查询</el-button>
        </div>
      </div>
      <div ref="tablebox" class="tablebox">
        <el-table
          :data="tableData"
          @select="handleSelectList"
          @sort-change="handleSortChange"
        >
          <!-- <el-table-column type="selection" width="55"/> -->
          <el-table-column prop="sort" label="排序" width="90" />
          <el-table-column prop="name" label="权限名称" min-width="100" />
          <el-table-column prop="type" label="权限类型">
            <template slot-scope="scope">
              <span v-if="scope.row.type == 1">菜单权限</span>
              <span v-if="scope.row.type == 2">资源权限</span>
            </template>
          </el-table-column>
          <el-table-column prop="pname" label="父权限" />
          <el-table-column prop="expression" label="权限表达式" min-width="90" />
          <el-table-column prop="path" label="访问路径" />
          <el-table-column prop="component" label="组件路径" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.component }}</template>
          </el-table-column>
          <el-table-column prop="httpMethod" label="请求方式" align="center" />
          <el-table-column prop="hidden" label="隐藏" width="90" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.type == 1">
                <span v-if="scope.row.hidden">是</span>
                <span v-else>否</span>
              </span>
              <span v-else>是</span>
            </template>
          </el-table-column>
          <el-table-column prop="describes" label="描述" min-width="120" show-overflow-tooltip />
          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button type="success" size="mini" @click="handleAdd(scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="handleDel([scope.row.id])">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-pagination
        background
        :page-sizes="[5, 10, 15, 30, 50, 100]"
        :page-size="pageCondition.size"
        :total="pageCondition.total"
        layout="total, prev, pager, next, sizes, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

      <!-- 新增弹出窗口 -->
      <el-dialog
        :title="dialogType"
        :visible.sync="addDialogShow"
        width="40%"
        :before-close="handleClose"
        :close-on-click-modal="false"
      >
        <div v-if="addDialogShow" class="dialog-content">
          <el-form
            ref="permissionform"
            :rules="rule"
            :model="permForm"
            label-width="95px"
            class="permissionform"
          >
            <el-form-item label="权限类型" prop="type">
              <el-select v-model="permForm.type" placeholder="请选择">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="permForm.type==2?'权限名称':'菜单名称'" prop="name">
              <el-input v-model="permForm.name" />
            </el-form-item>
            <el-form-item :label="permForm.type==2?'父权限':'上级菜单'" prop="pid">
              <el-select v-model="permForm.pid" filterable placeholder="请选择">
                <el-option
                  v-for="item in parentOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="permForm.type==2?'权限排序':'菜单排序'">
              <el-input-number v-model="permForm.sort" :min="0" :max="99" label="排序" />
            </el-form-item>
            <el-form-item v-if="permForm.type==1" label="是否隐藏">
              <el-select v-model="permForm.hidden" filterable placeholder="请选择">
                <el-option
                  v-for="item in hiddenOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item v-if="permForm.type==2" label="请求方式">
              <el-select v-model="permForm.httpMethod" filterable placeholder="请选择">
                <el-option
                  v-for="item in httpMethodOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item v-if="permForm.type==2" label="权限表达式">
              <el-input v-model="permForm.expression" />
            </el-form-item>
            <el-form-item v-if="permForm.type==1" label="图标">
              <el-input v-model="permForm.iconCls" />
            </el-form-item>
            <el-form-item label="访问路径">
              <el-input v-model="permForm.path" />
            </el-form-item>
            <el-form-item :label="permForm.type==2?'资源路径':'组件路径'">
              <el-input v-model="permForm.component" />
            </el-form-item>
            <el-form-item label="描述">
              <el-input
                v-model="permForm.describes"
                type="textarea"
                :autosize="{ minRows: 5, maxRows: 5}"
                placeholder="请输入描述"
              />
            </el-form-item>
          </el-form>
        </div>
        <div slot="footer">
          <el-button type="primary" @click="onSubmitPermission('permissionform')">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </div>

      </el-dialog>
    </div>
  </div>
</template>

<script>
import permApi from '@/api/system/permission'
export default {
  name: 'Role',
  data() {
    return {
      addDialogShow: false, // 新增弹出框是否显示
      dialogType: '新增',
      permForm: {},
      tableData: [], // 展现表格的数据
      filters: {},
      pageCondition: {
        type: '',
        page: 1,
        size: 15,
        sort: 'sort asc',
        total: 0
      }, // 分页条件
      permissionDialogShow: false, // 授权弹窗是否显示
      permissionAction: [], // 权限树结构
      roleId: '', // 所选角色的id
      checkedkeys: [], // 所选角色的权限
      parentOptions: [], // 父权限选择列表
      typeOptions: [
        {
          value: 1,
          label: '菜单权限'
        },
        {
          value: 2,
          label: '资源权限'
        }
      ], // 权限类型选择列表
      httpMethodOptions: [
        {
          value: 'GET',
          label: 'GET'
        },
        {
          value: 'POST',
          label: 'POST'
        },
        {
          value: 'PUT',
          label: 'PUT'
        },
        {
          value: 'DELETE',
          label: 'DELETE'
        }
      ], // 请求方式选择列表
      hiddenOptions: [
        {
          value: true,
          label: '是'
        },
        {
          value: false,
          label: '否'
        }
      ],
      rule: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        pid: [{ required: true, message: '请选择父权限', trigger: 'blur' }],
        type: [{ required: true, message: '请输入权限类型', trigger: 'blur' }]
      }
    }
  },
  watch: {
    filters: {
      deep: true,
      handler: function(newVal, oldVal) {
        this.buildQueryFilter()
      }
    }
  },
  created() {
    this.listPage()
  },
  methods: {
    // 获取权限(分页)
    listPage: function() {
      permApi.listPage(this.pageCondition).then(res => {
        this.tableData = res.data.content
        this.pageCondition.total = parseInt(res.data.totalElements)
      })
    },
    handleSortChange({ column, prop, order }) {
      // eslint-disable-next-line eqeqeq
      if (order == 'descending') {
        this.pageCondition.sort = prop + ' desc'
      } else {
        this.pageCondition.sort = prop + ' asc'
      }
      this.listPage()
    },
    // 获取菜单权限
    listAll: function() {
      const $this = this
      this.parentOptions = []
      permApi.listAll({ type: 1 }).then(res => {
        // eslint-disable-next-line eqeqeq
        const name = '顶级' + (this.permForm.type == 1 ? '菜单' : '权限')
        $this.parentOptions.push({ value: 0, label: name, text: name })
        res.data.forEach(item => {
          const name = item.name
          $this.parentOptions.push({ value: item.id, label: name, text: name })
        })
      })
    },
    // 每页显示数量调整
    handleSizeChange(val) {
      this.pageCondition.size = val
      this.listPage()
    },
    // 页面修改
    handleCurrentChange(val) {
      this.pageCondition.page = val
      this.listPage()
    },
    // 新增按钮---编辑
    handleAdd: function(row) {
      this.listAll()
      if (row) {
        this.permForm = { ...row }
        if (this.permForm.pid == '0') {
          this.permForm.pid = 0
        }
        this.dialogType = '编辑'
      } else {
        this.dialogType = '新增'
      }
      this.addDialogShow = true
    },
    // 新增或修改弹窗内事件
    // 关闭弹窗前执行的事件
    handleClose: function() {
      this.addDialogShow = false
      this.permForm = {}
    },
    // 点击确认新增或修改
    onSubmitPermission: function(fromName) {
      this.$refs[fromName].validate(valid => {
        if (valid) {
          // eslint-disable-next-line eqeqeq
          if (this.dialogType == '新增') {
            permApi.add(this.permForm).then(res => {
              // eslint-disable-next-line eqeqeq
              if (res.code == 200 && res.msg == 'ok') {
                this.handleClose()
                this.$notify({
                  title: '新增成功',
                  type: 'success'
                })
                this.listPage()
              } else {
                this.$notify.error({
                  title: '新增失败'
                })
              }
            })
          } else {
            permApi.edit(this.permForm).then(res => {
              // eslint-disable-next-line eqeqeq
              if (res.code == 200 && res.msg == 'ok') {
                this.handleClose()
                this.$notify({
                  title: '修改成功',
                  type: 'success'
                })
                this.listPage()
              } else {
                this.$notify.error({
                  title: '修改失败'
                })
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 多选框事件
    handleSelectList: function(selection, row) {
      this.selectList = selection.map(item => item.id)
    },
    // 删除
    handleDel: function(ids) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          permApi.deleteByIds({ ids }).then(res => {
            // eslint-disable-next-line eqeqeq
            if (res.code == 200 && res.msg == 'ok') {
              this.$notify({
                title: '删除成功',
                type: 'success'
              })
              this.listPage()
            } else {
              this.$notify.error({
                title: '删除失败'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 筛选
    doFilter: function() {
      this.buildQueryFilter()
      this.pageCondition.page = 1
      this.listPage()
    },
    buildQueryFilter: function() {
      const filter = this.filters
      this.pageCondition.type = filter.type
      this.pageCondition.pname = filter.pname
      this.pageCondition.hidden = filter.hidden
      this.pageCondition.name = filter.name
    }
  }
}
</script>
<style lang="scss">
  .permissionform {
    .el-select {
      width: 100%;
    }

    .el-input-number {
      width: 100%;
    }
  }
</style>
<style lang="scss" scoped>
  .app-container {
    .permission {
      background: #fff;
      height: 100%;
      overflow: hidden;

      .btn {
        padding: 0 0 16px 0;
        display: flex;
        justify-content: space-between;

        .right {
          max-width: calc(100% - 120px);
          display: flex;
          justify-content: flex-end;
        }
      }

      .tablebox {
        overflow: auto;
        margin-bottom: 16px;
      }

      .el-select {
        width: 100%;
      }
    }
  }
</style>
